module.exports = function accesstoken(){
    var request = require("request");
    var MongoClient = require('mongodb').MongoClient;
    var DB_CONN_STR = 'mongodb://weixinpc:Weixinpc*/66.@localhost:27017/weixin';
    // var DB_CONN_STR = 'mongodb://localhost:27017/weixin';
    request('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxab56a66099327643&secret=a741e6f82315be3d346e6ca054840bbf',function(err,res,body){        if(err){
            console.log(err);
        }else{
            var aaa = JSON.parse(body).access_token;
            console.log(aaa);
            var updateData = function(db, callback) {
                //连接到表 
                var collection = db.collection('site');
                //更新数据
                var whereStr = {"order":'111'};
                var updateStr = {$set: { "accesstoken" : aaa }};
                collection.update(whereStr,updateStr, function(err, result) {
                    if(err)
                    {
                        console.log('Error:'+ err);
                        return;
                    }   
                    callback(result);
                }); 
            }   
            
            MongoClient.connect(DB_CONN_STR, function(err, db) {
                updateData(db, function(result) {
                    db.close();
                }); 
            }); 
        }   
    })  
}